/**
 * 

 */
package edu.vt.ece.rt.quorum.util;

import java.util.Scanner;

import org.jgroups.Message;

import edu.vt.ece.rt.QR;
import edu.vt.ece.rt.quorum.Cluster;
import edu.vt.ece.rt.quorum.ClustersManager;
import edu.vt.ece.rt.quorum.messsages.types.Muter;

/**
 * To mute specific node
 * @author Sudhanshu
 *
 */
public class NodeReaper {

	static void mute(int baseId){
		try {
			Cluster c = ClustersManager.getCluster(Names.base);
			boolean b = c.getAutoReceive();
			c.setAutoReceive(true);
			c.send(new Message(null, new Muter(baseId)));
			c.setAutoReceive(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Mutes the specified nodes in configuration 
	 */
	public static void reap() {							//TODO: Implement while executing transactions
		if(Boolean.getBoolean(Names.reap)){
			String reapedNodes = System.getProperty(Names.reapedNodes);
			Scanner s = new Scanner(reapedNodes);
			s.useDelimiter("-");
			while(s.hasNextInt()){
				int i = s.nextInt();
				mute(i);
			}			
		}
	}
	
	public static void main(String[] args) throws Exception {
		QR.init();
		reap();
		QR.close();
	}

}
